package m202405.d14;

import java.util.PriorityQueue;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/14 19:19
 */
public class Q295 {

    public static void main(String[] args) {

    }

    public static class MedianFinder {

        //后半部分,小顶堆
        PriorityQueue<Integer> small;
        //前半部分，大顶堆
        PriorityQueue<Integer> big;

        public MedianFinder() {
            small = new PriorityQueue<>((v1, v2) -> v1 - v2);
            big = new PriorityQueue<>((v1, v2) -> v2 - v1);
        }

        public void addNum(int num) {
            if (big.isEmpty() || num >= big.peek()) {
                small.add(num);
                while (big.size() < small.size() - 1) {
                    big.add(small.poll());
                }
                return;
            }
            if (small.isEmpty() || num < small.peek()) {
                big.add(num);
                while (small.size() < big.size()) {
                    small.add(big.poll());
                }
            }
        }

        public double findMedian() {
            if (small.size() > big.size()) {
                return small.peek();
            } else if (small.size() < big.size()) {
                return big.peek();
            } else {
                return (small.peek() + big.peek()) / 2.0;
            }
        }
    }
}
